/* $Id: UserDAOImpl.java 
 * Copyright (C) 2011, TRAKHEES
 *  
 * Revision History
 *
 * Date  			Author 		    	Remarks
 * ---------------------------------------------------------------------------------
 *
 */
package ae.pcfc.etks.online.common.dao.provider;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.util.StringUtils;

import ae.pcfc.etks.online.common.ConstantsCommon;
import ae.pcfc.etks.online.common.dao.UserDAO;
import ae.pcfc.etks.online.common.domain.User;
import ae.pcfc.etks.online.common.vo.PagingVO;

public class UserDAOImpl extends BaseDAO implements UserDAO {

	/**
	 * findUserByUserName
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#findUserByUserName(java.lang.String)
	 * 
	 */
	public User findUserByUserName(String strUserName) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.add(Restrictions.eq("user.UUsername", strUserName).ignoreCase());
		
		List<User> list = criteria.list();
		
		return (list != null && list.size() > 0)?list.get(0):null;
	}

	/**
	 * getUserList
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#getUserList()
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<User> getUserList() {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		
		//criteria.add(Restrictions.ne("UType", "A"));
		
		return criteria.list();
	}

	/**
	 * getUserListByPagingParameters
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#getUserListByPagingParameters(ae.pcfc.etks.online.common.vo.PagingVO)
	 * 
	 */
	public List<User> getUserListByPagingParameters(PagingVO pagingVO) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		
		if(pagingVO.getSearchfieldName() != null && StringUtils.hasText(pagingVO.getSearchfieldName()))
		{
			if(pagingVO.getSerachString() !=null && StringUtils.hasText(pagingVO.getSerachString()))
			{
				if(pagingVO.getSearchfieldName().equals("UId"))
				{
					criteria.add(Restrictions.eq("user."+pagingVO.getSearchfieldName(), Long.parseLong(pagingVO.getSerachString())));
				}else
				criteria.add(Restrictions.like("user."+pagingVO.getSearchfieldName(), pagingVO.getSerachString()));
			}
			
		}
		if(pagingVO.getClientId() != null && StringUtils.hasText(pagingVO.getClientId()))
		{
			criteria.add(Restrictions.eq("user.client.CId", Long.parseLong(pagingVO.getClientId())));
		}		
		if(pagingVO.getSortFieldName() != null)
		{
			if(pagingVO.getSortOrder().equals("asc"))
			{
				criteria.addOrder(Order.asc(pagingVO.getSortFieldName()));
				
			}
			else if(pagingVO.getSortOrder().equals("desc"))
			{
				
				criteria.addOrder(Order.desc(pagingVO.getSortFieldName()));
			}
		}
		
		//criteria.add(Restrictions.ne("UType", "A")); 
		
		if(pagingVO.getPageNumber() != null)
		criteria.setFirstResult(pagingVO.getStart());
		
		if(pagingVO.getPageSize() != null)
		criteria.setMaxResults(pagingVO.getPageSize());
		
		@SuppressWarnings("unchecked")
		List<User> users = criteria.list();
		
		return (users != null && users.size() > 0)? users: null ;
	}

	/**
	 * finByPrimaryKey
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#finByPrimaryKey(java.lang.Long)
	 * 
	 */
	public User finByPrimaryKey(Long id) {
		return (User) getHibernateTemplate().getSessionFactory().getCurrentSession().get(User.class, id);
		
	}

	/**
	 * getUserByEmail
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#getUserByEmail(java.lang.String)
	 * 
	 */
	public User getUserByEmail(String email) {
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.add(Restrictions.like("user.UEmail", email).ignoreCase());
		List<User> users = criteria.list();
		
		return (users != null && users.size() > 0 ) ? users.get(0) : null;
	}

	/**
	 * add
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#add(ae.pcfc.etks.online.common.domain.User)
	 * 
	 */
	public void add(User user) {
		
		getHibernateTemplate().getSessionFactory().getCurrentSession().save(user);
		
	}

	/**
	 * update
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#update(ae.pcfc.etks.online.common.domain.User)
	 * 
	 */
	public void update(User user) {
		
		getHibernateTemplate().getSessionFactory().getCurrentSession().saveOrUpdate(user);
		
	}
	
	/**
	 * findUserByEmailExceptCurrentUser
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#findUserByEmailExceptCurrentUser(java.lang.String, java.lang.Long)
	 * 
	 */
	public User findUserByEmailExceptCurrentUser(String strEmail, Long lUserId) {
		// TODO Auto-generated method stub
		
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.add(Restrictions.like("user.UEmail", strEmail).ignoreCase());
		criteria.add(Restrictions.ne("user.UId", lUserId));
		List<User> users = criteria.list();
		
		return (users != null && users.size() > 0 ) ? users.get(0) : null;
	}

	/**
	 * findUserByEmailExceptCurrentCompany
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#findUserByEmailExceptCurrentCompany(java.lang.String, java.lang.String)
	 * 
	 */
	public User findUserByEmailExceptCurrentCompany(String strEmail,
			String strTrakheesId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.add(Restrictions.like("user.UEmail", strEmail).ignoreCase());
		criteria.createAlias("user.client", "client");
		if(strTrakheesId != null){
			criteria.add(Restrictions.ne("client.CTrakheesId", strTrakheesId).ignoreCase());
		}else{
			criteria.add(Restrictions.isNull("client.CTrakheesId"));
		}
		List<User> users = criteria.list();
		
		return (users != null && users.size() > 0 ) ? users.get(0) : null;
	}
	
	
	/**
	 * findUsersByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#findUsersByTrakheesId(java.lang.String)
	 * 
	 */
	public List<User> findUsersByTrakheesId(String strTrakheesId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.createAlias("user.client", "client");
		criteria.add(Restrictions.eq("client.CTrakheesId", strTrakheesId).ignoreCase());
		List<User> users = criteria.list();
		
		return users;
	}

	/**
	 * findClientAdminByTrakheesId
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#findClientAdminByTrakheesId(java.lang.String)
	 * 
	 */
	public User findClientAdminByTrakheesId(String strTrakheesId) {
		Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(User.class, "user");
		criteria.add(Restrictions.eq("user.UType", ConstantsCommon.COMPANY_ADMIN).ignoreCase());
		criteria.createAlias("user.client", "client");
		if(strTrakheesId != null){
			criteria.add(Restrictions.eq("client.CTrakheesId", strTrakheesId).ignoreCase());
		}else{
			return null;
		}
		List<User> users = criteria.list();
		
		return (users != null && users.size() > 0 ) ? users.get(0) : null;
	}

	/**
	 * delete
	 * 
	 * @see ae.pcfc.etks.online.common.dao.UserDAO#delete(ae.pcfc.etks.online.common.domain.User)
	 * 
	 */
	public void delete(User user) {
	    getHibernateTemplate().getSessionFactory().getCurrentSession().delete(user);
	}	

}
